package top.wshape1.shiziapi.message.context;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2025-02-07
 */

@Component
@Slf4j
public class MachineNodeContextHandler {


    // 在分布式环境中，这个ID需要保证唯一
    public String MACHINE_ID;

    @Value("${server.port:}")
    private int serverPort;

    @PostConstruct
    public void postConstruct() {
        try {
            MACHINE_ID = InetAddress.getLocalHost().getHostAddress() + "_" + serverPort + "_" + UUID.randomUUID().toString().replace("-", "");
            log.info("初始化机器ID: {}", MACHINE_ID);
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

}
